/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import hibernate.entity.Account;
import hibernate.entity.Role;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.transform.Transformers;

/**
 *
 * @author NamDH
 */
public class AccountDAO {

    private Session session;

    public AccountDAO() {
        SessionFactory sf = Common.HibernateUtil.getSessionFactory();
        session = sf.getCurrentSession();
    }

    public Account getAccountforLogin(String account_Email, String account_Password) {
        Account account = new Account();
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "select a.emailAddress, a.role from Account a Where a.emailAddress = ? and a.password = ?";
            Query query = session.createQuery(sql).setResultTransformer(Transformers.aliasToBean(Account.class));
            query.setString(0, account_Email);
            query.setString(1, account_Password);
            //Object acc = query.uniqueResult();

            //   if(account!=null)
            Hibernate.initialize(account.getRole());
            session.getTransaction().commit();
            return (Account) query.uniqueResult();

        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return null;
        } finally {
            session.flush();
            session.getTransaction().commit();
        }
        //return account;
    }

    public boolean changePassword(String password, int accountID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            Account acc = (Account) session.get(Account.class, accountID);
            acc.setPassword(password);
            session.update(acc);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }

    public Account checkAccountByPassword(String account_Email, String account_Password) {
        Account account = new Account();
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "from Account Where EmailAddress = ? and Password = ?";
            Query query = session.createQuery(sql);
            query.setString(0, account_Email);
            query.setString(1, account_Password);
            //Object acc = query.uniqueResult();
            account = (Account) query.uniqueResult();

            if (account != null) {
                Hibernate.initialize(account.getRole());
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return null;
        }
        return account;
    }

    public Account checkAccountByUsername(String account_Email, String account_Username) {
        Account account = new Account();
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "from Account Where EmailAddress = ? and Username = ?";
            Query query = session.createQuery(sql);
            query.setString(0, account_Email);
            query.setString(1, account_Username);
            //Object acc = query.uniqueResult();
            account = (Account) query.uniqueResult();
            if (account != null) {
                Hibernate.initialize(account.getRole());
            }
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return null;
        }
        return account;
    }

    public List<Account> getAllAccount() {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "from Account";
            Query query = session.createQuery(sql);
            List<Account> result = query.list();
            session.getTransaction().commit();
            return result;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public List<Account> searchAccount(String search) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "from Account Where EmailAddress like ? OR Username like ?";
            Query query = session.createQuery(sql);
            query.setString(0, "%" + search + "%");
            query.setString(1, "%" + search + "%");
            List<Account> result = query.list();
            session.getTransaction().commit();
            return result;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public Account getAccountById(int accountID) {
        Account account = new Account();
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "from Account Where AccountID like ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, accountID);
            account = (Account) query.uniqueResult();
            if (account != null) {
                Hibernate.initialize(account.getRole());
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return null;
        }
        return account;
    }

    public Account checkEmail(String emailAddress) {
        Account account = new Account();
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "from Account Where EmailAddress like ?";
            Query query = session.createQuery(sql);
            query.setString(0, emailAddress);
            account = (Account) query.uniqueResult();
            if (account != null) {
                Hibernate.initialize(account.getRole());
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return null;
        }
        return account;
    }

    public boolean updateActive(boolean active, int accountID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            Account acc = (Account) session.get(Account.class, accountID);
            acc.setActive(active);
            session.update(acc);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }

    public boolean updateEmailVerify(boolean active, int accountID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            Account acc = (Account) session.get(Account.class, accountID);
            acc.setEmailVerify(active);
            session.update(acc);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }

    public boolean updateRole(int roleID, int accountID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            Account acc = (Account) session.get(Account.class, accountID);
            Role role = new Role();
            role.setRoleID(roleID);
            acc.setRole(role);
            session.update(acc);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }

    public boolean updateFullName(String username, int accountID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            Account acc = (Account) session.get(Account.class, accountID);
            acc.setUsername(username);
            session.update(acc);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }

    public boolean delete(int accountID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            Account acc = (Account) session.get(Account.class, accountID);
            session.delete(acc);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }

    public int register(int roleID, String emailAddress, String username, String password, boolean active, boolean emailVerify) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            Role role = (Role) session.get(Role.class, roleID);
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date date = new Date();
            String signupDate = dateFormat.format(date);
            Account acc = new Account(role, emailAddress, username, password, signupDate, active, emailVerify);
            session.save(acc);
            session.flush();
            session.getTransaction().commit();
            int accID = acc.getAccountID();
            return accID;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    

   

  

   

    public int countAccount() {
        int result = 0;
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            Number number = (Number) session.createCriteria(Account.class).setProjection(Projections.rowCount()).uniqueResult();
            result = number.intValue();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return result;
    }
    //thuc chat la ham check Email fia tren nhung ham checkEmail dat ten k dung
    //y nghia nen viet lai ham dat ten khac
    public Account getAccountByEmail(String email){
         Account account = new Account();
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "from Account Where EmailAddress = ?";
            Query query = session.createQuery(sql);
            query.setString(0, email);
            account = (Account) query.uniqueResult();
            if (account != null) {
                Hibernate.initialize(account.getRole());
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return null;
        }
        return account;
    }
}
